-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
164 lines (139 loc) · 6.99 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Api extends CI_Controller {
function __construct() {
//TODO: all validations should go here, numbers texts everything should be validated
parent::__construct();
switch ($this->router->fetch_method()) {
case "add":
$json = json_decode(trim(file_get_contents('php://input')));
if ($json->latitude == ""
|| $json->longitude == ""
|| $json->text == "") {
echo json_encode(array("error"=>"Cannot add new message"));
exit;
}
break;
case "get":
break;
case "get_map_data":
break;
}
}
public function index() {
// Silence is golden.
exit;
}
private function server_time() {
return time();
}
public function get_server_time() {
echo $this->server_time();
}
public function add() {
$current_time = $this->server_time();
$json = json_decode(trim(file_get_contents('php://input')));
$data = array(
'latitude' => $json->latitude,
'longitude' => $json->longitude,
'text' => $json->text,
'pubTime' => $current_time + ($json->pubDelay ? $json->pubDelay : 0),
'parent_id' => $json->parent_id ? $json->parent_id : 0
);
$this->db->insert('skene_messages', $data);
// Return the ID of the newly inserted message
echo $this->db->insert_id();
}
public function get() {
// Available parameters:
// lat: center location latitude
// long: center location longitude
// radius: radius of area in meters
// timestamp (default: 0): get messages posted after this time. Format: Unix timestamp
// count (default: 50): maximum number of messages to return
// parent_id: (default 0): only get messages with this parent_id
$timestamp = ($this->input->get('timestamp')) ? $this->input->get('timestamp') : 0;
$count = ($this->input->get('count')) ? $this->input->get('count') : 50;
$parent_id = ($this->input->get('parent_id')) ? $this->input->get('parent_id') : 0;
$have_specifics = false;
if($this->input->get('radius') && $this->input->get('lat') && $this->input->get('long')) {
$radius = $this->input->get('radius') / 1000; //to km
$lat = $this->input->get('lat');
$long = $this->input->get('long');
// Get min/max latitude and longitue to select messages from the database
$R = 6371; // earth's mean radius, km
// First-cut bounding box (in degrees)
$max_lat = $lat + rad2deg($radius/$R);
$min_lat = $lat - rad2deg($radius/$R);
// Compensate for degrees longitude getting smaller with increasing latitude
$max_long = $long + rad2deg($radius/$R/cos(deg2rad($lat)));
$min_long = $long - rad2deg($radius/$R/cos(deg2rad($lat)));
$have_specifics = true;
}
$current_time = $this->server_time();
// Compose SQL query
if ($parent_id == 0 && $have_specifics) {
// Get skenes by location
$query = $this->db->order_by("id", "desc")->get_where('skene_messages',
array('parent_id = ' => 0,
'latitude >=' => $min_lat,
'latitude <=' => $max_lat,
'longitude >=' => $min_long,
'longitude <=' => $max_long,
'pubTime >=' => $timestamp,
'pubTime <=' => $current_time),
$count);
} elseif ($parent_id == 0) {
// Get latest skenes from the entire world
$query = $this->db->order_by("id", "desc")->get_where('skene_messages',
array('parent_id = ' => 0,
'pubTime >=' => $timestamp,
'pubTime <=' => $current_time),
$count);
} else {
// Get by parent ID
$query = $this->db->order_by("id", "desc")->get_where('skene_messages',
array('parent_id = ' => $parent_id,
'pubTime >=' => $timestamp,
'pubTime <=' => $current_time),
$count);
}
// Run database query
$result = $query->result();
// Output in JSON format
echo json_encode($result);
}
public function get_map_data() {
// Get N latest conversations.
// TODO: Try to filter not by first message date, but by last reply date - tricky?
// Available parameters:
// min_lat: min/max lat/long of message
// max_lat: --
// min_long: --
// max_long: --
// timestamp (default: 0): get messages posted after this time. Format: Unix timestamp
// count (default: 50): maximum number of messages to return
// Get required parameters
$min_lat = $this->input->get('min_lat');
$max_lat = $this->input->get('max_lat');
$min_long = $this->input->get('min_long');
$max_long = $this->input->get('max_long');
// Get optional parameters
$timestamp = ($this->input->get('timestamp')) ? $this->input->get('timestamp') : 0;
$count = ($this->input->get('count')) ? $this->input->get('count') : 50;
$current_time = $this->server_time();
// Compose SQL query
$query = $this->db->order_by("id", "desc")->select('latitude, longitude, pubTime')->get_where('skene_messages',
array('parent_id = ' => 0,
'latitude >=' => $min_lat,
'latitude <=' => $max_lat,
'longitude >=' => $min_long,
'longitude <=' => $max_long,
'pubTime >=' => $timestamp,
'pubTime <=' => $current_time),
$count);
// Run database query
$result = $query->result();
// Output in JSON format
echo json_encode($result);
}
}